<?php
class Setting extends Admin_Controller{

	protected $table;
	protected $file_name;

	public function __construct(){
		parent::__construct();
		$this->load->model(array("Msetting"));
		$this->table = 'tpt_setting';
		$this->file_name = 'page_config';
		$this->breadcrumbs->push('Setting', 'admincp/setting/view');
	}

	public function view(){
		$data['icon_page']=$this->config->item('icon_view');
        $data['title']='Setting Manager';
        $data['sub_title']='Setting Manager';
        $data['columns'] = array(
        	'id' => trans('id'),
        	'keywords' => trans('keywords'),
        	'value' => trans('value'),
        	'created_at' => trans('created_at')
        );
        $data['settings']=$this->Msetting->listSetting(array_keys($data['columns']));
		$this->parser->parse("setting/view.tpl", NULL, $data);
	}

	public function add(){
		$data['icon_page']=$this->config->item('icon_create');
        $data['title']='Create New Setting';
        $data['sub_title']='Create New Setting';
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_insert = array(
				'title' => $this->input->post('title'),
				'keywords' => $this->input->post('keywords'),
				'value' => $this->input->post('value'),
				'created_at' => time()
			);
			$this->Msetting->insert($data_insert);
			save_file_setting($this->table, $this->file_name);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(admincp_url()."setting/view");
		}
		$this->breadcrumbs->push('Create Setting', 'admincp/setting/add');
		$this->parser->parse("setting/add.tpl", NULL, $data);
	}

	public function edit(){
		$data['icon_page']=$this->config->item('icon_edit');
        $data['title']='Edit Setting';
        $data['sub_title']='Edit Setting';
		$cache_id = $id = $this->uri->segment(4);
		$data['info'] = $this->Msetting->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$data_update = array(
				'title' => $this->input->post('title'),
				'value' => $this->input->post('value'),
				'updated_at' => time()
			);
			$this->Msetting->update($data_update,$id);
			save_file_setting($this->table, $this->file_name);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(admincp_url()."setting/view");		
		}
		$this->breadcrumbs->push('Edit Setting', 'admincp/setting/edit/'.$id);
		$this->parser->parse("setting/edit.tpl", $cache_id, $data);
	}

	public function delete(){
		$id = $this->uri->segment(4);
		$this->Msetting->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(admincp_url()."setting/view");
	}
	
	// public function index(){
	// 	$this->breadcrumb->append_crumb('Home',admincp_url());
	// 	$this->breadcrumb->append_crumb('Setting',base_url().'admincp/setting');
	// 	$data['icon_page']=$this->config->item('icon_setting');
 //        $data['title_page']='Setting Manager';
 //        $data['sub_title_page']='Setting Manager';
	// 	$data['info'] = $this->Msetting->get_data();
	// 	$path_upload = 'assets/filemanager/logo/';
	// 	$config['upload_path'] = './'.$path_upload;
	// 	$config['allowed_types'] = 'jpg|png';
	// 	$config['max_size']	= '1000';
	// 	$config['max_width']  = '1024';
	// 	$config['max_height']  = '768';
	// 	$this->load->library('upload', $config);
	// 	if($this->input->server('REQUEST_METHOD') == "POST"){
	// 		$img=$this->upload->data();
	// 		$this->load->library('image_lib'); 
	// 		$config['image_library'] = 'gd2';
	// 		$config['source_image']	= $path_upload.$img['file_name'];
	// 		$config['create_thumb'] = FALSE;
	// 		$config['maintain_ratio'] = TRUE;
	// 		$config['width']	 = 75;
	// 		$config['height']	= 50;
	// 		$this->image_lib->initialize($config);
	// 		$this->image_lib->resize();
	// 		$data = array(
	// 			'setting_title' => $this->input->post('setting_title'),
	// 			'setting_keywords' => $this->input->post('setting_keywords'),
	// 			'setting_description' => $this->input->post('setting_description'),
	// 			'setting_public' => $this->input->post('setting_public'),
	// 			// 'setting_slogan' => $this->input->post('setting_slogan'),
	// 			// 'setting_hotline' => $this->input->post('setting_hotline'),
	// 			'setting_logo' => $path_upload.$img['file_name'],
	// 			// 'setting_footer' => $this->input->post('setting_footer'),
	// 		);
	// 		if($this->Msetting->count_row() == 0){		
	// 			$this->Msetting->insert($data);
	// 		}else{
	// 			$this->Msetting->update($data);
	// 		}
	// 		save_file_setting($this->table, $this->file_name);
	// 		$this->session->set_flashdata(array("success"=>trans('message_save_successfully')));
	// 		redirect(base_url().'admincp');
	// 	}
	// 	$this->parser->parse("setting/index.tpl", NULL, $data);
	// }

}